package co.yixiang.modules.product.service.impl;

import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.modules.product.domain.YxBusinessDetail;
import co.yixiang.modules.product.domain.YxBusinessTitle;
import co.yixiang.modules.product.service.YxBusinessDetailService;
import co.yixiang.modules.product.service.YxBusinessTitleService;
import co.yixiang.modules.product.service.dto.YxBusinessDetailDto;
import co.yixiang.modules.product.service.dto.YxBusinessDetailQueryCriteria;
import co.yixiang.modules.product.service.mapper.YxBusinessDetailMapper;
import co.yixiang.modules.product.vo.YxBusinessDetailQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import co.yixiang.dozer.service.IGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;

import co.yixiang.domain.PageResult;
/**
* @author lsh
* @date 2021-06-21
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxBusinessDetail")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxBusinessDetailServiceImpl extends BaseServiceImpl<YxBusinessDetailMapper, YxBusinessDetail> implements YxBusinessDetailService {

    private final IGenerator generator;

    @Autowired
    private YxBusinessTitleService yxBusinessTitleService;

    @Override
    //@Cacheable
    public PageResult<YxBusinessDetailDto> queryAll(YxBusinessDetailQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxBusinessDetail> page = new PageInfo<>(queryAll(criteria));
        return generator.convertPageInfo(page,YxBusinessDetailDto.class);
    }


    @Override
    //@Cacheable
    public List<YxBusinessDetail> queryAll(YxBusinessDetailQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxBusinessDetail.class, criteria));
    }


    @Override
    public void download(List<YxBusinessDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxBusinessDetailDto yxBusinessDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标题图片", yxBusinessDetail.getImage());
            map.put("轮播图", yxBusinessDetail.getSliderImage());
            map.put("标题名称", yxBusinessDetail.getTitleName());
            map.put("标题简介", yxBusinessDetail.getTitleInfo());
            map.put("关键字", yxBusinessDetail.getKeyword());
            map.put("分类id", yxBusinessDetail.getBusinessTitleId());
            map.put("排序", yxBusinessDetail.getSort());
            map.put("销量", yxBusinessDetail.getSales());
            map.put("标题描述", yxBusinessDetail.getDescription());
            map.put("添加时间", yxBusinessDetail.getCreateTime());
            map.put(" updateTime",  yxBusinessDetail.getUpdateTime());
            map.put("是否删除", yxBusinessDetail.getIsDel());
            map.put("浏览量", yxBusinessDetail.getBrowse());
            map.put("级别解锁", yxBusinessDetail.getSingular());
            map.put("普通话版语音", yxBusinessDetail.getVoiceUrl());
            map.put("维话版语音", yxBusinessDetail.getUighurVoiceUrl());
            map.put("普通话版视频", yxBusinessDetail.getVideoUrl());
            map.put("维话版视频", yxBusinessDetail.getVideoVoiceUrl());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    /**
     * 订单列表
     * @param businessTitleId 标题id
     * @return list
     */
    @Override
    public List<YxBusinessDetail> getDetail(Long businessTitleId) {
        LambdaQueryWrapper<YxBusinessDetail> wrapper= new LambdaQueryWrapper<>();
        if(businessTitleId != null) {
            wrapper.eq(YxBusinessDetail::getBusinessTitleId,businessTitleId);
        }
        wrapper.orderByDesc(YxBusinessDetail::getSort);
        List<YxBusinessDetail> detailList = baseMapper.selectList(wrapper);
        YxBusinessTitle title =yxBusinessTitleService.getById(businessTitleId);
        Integer browse =title.getBrowse()+1;
        title.setBrowse(browse);
        yxBusinessTitleService.updateById(title);
        return detailList;
    }
}
